<?php

namespace App\Admin\Tools\Task;

use App\Models\AdminUsersRelatedModel;
use Encore\Admin\Grid\Tools\AbstractTool;
use Illuminate\Support\Facades\Auth;

class TaskAllocationTool extends AbstractTool
{
    private int $id;
    private array $data;

    public function __construct(int $id, array $data = [])
    {
        $this->id = $id;
        $this->data = $data;
    }

    /**
     * @inheritDoc
     */
    public function render()
    {
        $model = AdminUsersRelatedModel::query()
            ->leftJoin('admin_users', 'admin_users_related.user_id', 'admin_users.id');
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) {
                $model->orWhere('admin_users_related.main_id', MAINID)
                    ->orWhere('admin_users_related.user_id', MAINID);
            } else $model->where('admin_users_related.main_id', MAINID);
        }
        $model = $model->pluck('name', 'user_id')->toArray();

        return view('task.allocation', [
            'id' => $this->id,
            'user' => $model,
            'data' => $this->data,
        ]);
    }
}
